#include "Container.h"
#include "Control.h"
#include "../debug_new.h"
#include "Util.h"

CControl::CControl(UINT nID)
:m_pContainer(NULL),
m_rcControl		 (NULL),
m_dwStyle	    (SBS_RECT),
m_nID		    (nID),
m_pFont		    (NULL),
m_nState		 (STATE_NORMAL)
{
	m_bEnabled = TRUE;
	m_bVisibale = TRUE;
}

CControl::CControl(wstring strCaption,UINT nID)
:m_pContainer(NULL),
m_strCaption    (strCaption),
m_rcControl		 (NULL),
m_dwStyle	    (SBS_RECT),
m_nID		    (nID),
m_pFont		    (NULL),
m_nState		 (STATE_NORMAL)
{
	m_bEnabled = TRUE;
	m_bVisibale = TRUE;
}

CControl::CControl(DWORD dwStyle, wstring strCaption,const CRect rc, UINT nID)
:m_pContainer(NULL),
m_rcControl		 (rc),
m_dwStyle	    (dwStyle),
m_nID		    (nID),
m_pFont		    (NULL),
m_nState		 (STATE_NORMAL),
m_strCaption    (strCaption)
{
	m_bEnabled = TRUE;
	m_bVisibale = TRUE;
	m_rcControlCopy=m_rcControl;
}


CControl::CControl(DWORD dwStyle, wstring strCaption, int x, int y, int cx, int cy, UINT nID)
:m_pContainer(NULL),
m_rcControl	    (x, y, x + cx, y + cy),
m_dwStyle	    (dwStyle),
m_nID		    (nID),
m_pFont		    (NULL),
m_strCaption    (strCaption),
m_nState		 (STATE_NORMAL)
{
	m_bEnabled = TRUE;
	m_bVisibale = TRUE;
	m_rcControlCopy=m_rcControl;
}

CControl::~CControl(void)
{
	m_strCaption.clear();
}

/*
void CControl::SetCaption(wstring strCaption)
{
	m_strCaption = strCaption;

	if(IsWindow(m_pContainer->m_hWnd))
	{
		InvalidateRect(m_pContainer->m_hWnd,&m_rcControl, FALSE);
	}
}
*/

void CControl::Show(BOOL bShow)
{
	m_bVisibale = bShow;
}

void CControl::Click(DWORD dwClick, POINT pt)
{
	if(!IsEnabled())
	{
		return;
	}

	if(!m_bVisibale)
	{
		return;
	}

	if(!m_pContainer || !m_pContainer->m_hWnd)
	{
		return;
	}

	if(m_rcControl.PtInRect(pt))
	{
		if(dwClick & CLICK_DOWN)
		{
			m_nState = STATE_PUSHED;
		}
		else if(dwClick & CLICK_UP)
		{
			if(m_pContainer->m_hWnd)
			{
				CUtil::PlayBeepSound();
				PostMessage(m_pContainer->m_hWnd,WM_COMMAND, m_nID,NULL);
			}
			m_nState = STATE_NORMAL;
		}
	}
	else
	{
		m_nState = STATE_NORMAL;
	}

	if(m_pContainer && m_pContainer->m_hWnd)
	{
		CClientDC dc(m_pContainer->m_hWnd);
		Paint(&dc);
	}
}

void CControl::PaintBackGnd(CDC* pDC)
{
	if ( NULL==m_pContainer )
	{
		m_pContainer->PaintBackGround( pDC->m_hDC,NULL );
	}

	if(m_pContainer->GetBackGnd())
	{
		CDC tmpDC;
		tmpDC.CreateCompatibleDC(NULL);
		HBITMAP hbmpOld = tmpDC.SelectBitmap(m_pContainer->GetBackGnd());
		pDC->BitBlt(m_rcControl.left,m_rcControl.top,m_rcControl.Width(),m_rcControl.Height()
			,tmpDC.m_hDC,m_rcControl.left,m_rcControl.top,SRCCOPY);
		tmpDC.SelectBitmap( hbmpOld );
		tmpDC.DeleteDC();	
	}
	else
	{
		pDC->FillSolidRect(m_rcControl,m_pContainer->GetBackColor());
	}
}

void CControl::Paint(CDC* pDC)
{
	PaintBackGnd(pDC);
}

void CControl::Enable(BOOL bEnable)
{
	m_bEnabled = bEnable;
	if(!bEnable)
	{
		m_nState = STATE_DISABLED;
	}
	else
	{
		m_nState = STATE_NORMAL;
	}
}